!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
subroutine write_mapvis(lv, nc, dx, dy, x, y, ro, gp, mp, te, beta, az, vx, vy, vz, bx, by, bz, jx, jy, jz,                        &
   fgx, fgy, fggx, fggy, fmgx, fmgy, fmtx, fmty, fmtz, var_flag, nx, ny, ng, n_var_total, output_unit)
!==================================================================================================================================|

   implicit none

   include 'data_manage_precision.include'

   integer(4), intent(in) :: lv, nc, nx, ny, ng, n_var_total, output_unit
   logical(4), intent(in) :: var_flag(n_var_total)
   real(8), intent(in) :: dx, dy
   real(8), intent(in) :: x(nx), y(ny)
   real(output_real8_flag), dimension(nx, ny), intent(in) :: ro, gp, mp, te, beta, az, vx, vy, vz, bx, by, bz,                     &
      jx, jy, jz, fgx, fgy, fggx, fggy, fmgx, fmgy, fmtx, fmty, fmtz

   integer(4) :: i_var

!----------------------------------------------------------------------------------------------------------------------------------|
!  ro, gp, mp, te, beta, az, vx, vy, vz, vv, bx, by, bz, bv, jx, jy, jz, jv, fgx, fgy, fgv, fggx, fggy, fggv, 
!  fmgx, fmgy, fmgv, fmtx, fmty, fmtz, fmtv
!----------------------------------------------------------------------------------------------------------------------------------|
   write(output_unit) lv, nc
   write(output_unit) dx, dy
   write(output_unit) x(ng : nx - ng)
   write(output_unit) y(ng : ny - ng)

   i_var = 1
   if (var_flag(i_var)) write(output_unit) ro(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) gp(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) mp(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) te(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) beta(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) az(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) vx(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) vy(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) vz(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) then
      write(output_unit) vx(ng : nx - ng, ng : ny - ng)
      write(output_unit) vy(ng : nx - ng, ng : ny - ng)
   endif
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) bx(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) by(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) bz(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) then
      write(output_unit) bx(ng : nx - ng, ng : ny - ng)
      write(output_unit) by(ng : nx - ng, ng : ny - ng)
   endif
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) jx(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) jy(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) jz(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) then
      write(output_unit) jx(ng : nx - ng, ng : ny - ng)
      write(output_unit) jy(ng : nx - ng, ng : ny - ng)
   endif
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) fgx(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) fgy(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) then
      write(output_unit) fgx(ng : nx - ng, ng : ny - ng)
      write(output_unit) fgy(ng : nx - ng, ng : ny - ng)
   endif
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) fggx(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) fggy(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) then
      write(output_unit) fggx(ng : nx - ng, ng : ny - ng)
      write(output_unit) fggy(ng : nx - ng, ng : ny - ng)
   endif
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) fmgx(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) fmgy(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) then
      write(output_unit) fmgx(ng : nx - ng, ng : ny - ng)
      write(output_unit) fmgy(ng : nx - ng, ng : ny - ng)
   endif
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) fmtx(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) fmty(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) write(output_unit) fmtz(ng : nx - ng, ng : ny - ng)
   i_var = i_var + 1
   if (var_flag(i_var)) then
      write(output_unit) fmtx(ng : nx - ng, ng : ny - ng)
      write(output_unit) fmty(ng : nx - ng, ng : ny - ng)
   endif

!----------------------------------------------------------------------------------------------------------------------------------|
   return
end subroutine write_mapvis
